package com.example.a10.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.a10.model.entity.ResultVo;
import com.example.a10.mapper.CosManager;
import com.example.a10.newentity.resultFile;
import com.example.a10.service.resultFileService;
import com.example.a10.model.entity.Document;
import com.example.a10.service.JsonServer;
import com.example.a10.util.optionJsonUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qcloud.cos.model.COSObjectInputStream;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParser;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Result")

public class ResultController {

    @Autowired
    HttpServletResponse response;
    @Autowired
    JsonServer jsonServer;
    @Autowired
    private JsonParser jsonParser;
    @Autowired
    resultFileService resultFileservice;
    @Autowired
    CosManager cosManager;

    @ApiOperation("返回所有结果文件的名称")
    @GetMapping("/list")
    public List<resultFile> list(String userid){
        LambdaQueryWrapper<resultFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(resultFile::getUserId,userid);
        List<resultFile> resultFileList = resultFileservice.list(lambdaQueryWrapper);
        return resultFileList;
    }

    @ApiOperation("接受结果文件名称,下载结果文件")
    @PostMapping("/download")
    public void Download(@RequestBody resultFile resultFile) throws IOException {
        String url = resultFile.getUrl();
        COSObjectInputStream cosObjectInputStream = cosManager.downloadFile(url);
        // 设置响应头信息
        response.setContentType("application/octet-stream"); // 设置响应类型为二进制流
        // 使用 ServletUtil.write() 方法将文件内容写入 ServletOutputStream
        ServletUtil.write(response, cosObjectInputStream);
    }

    @ApiOperation("接受结果文件名称,返回结果文件数据")
    @PostMapping("/resultlist")
    public ResultVo getJsonData(@RequestBody Document document) throws IOException {
        String url = document.getUrl();
        COSObjectInputStream cosObjectInputStream = cosManager.downloadFile(url);

        // 使用 InputStreamReader 读取输入流，并转换为字符串
        BufferedReader reader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        String jsonContent = stringBuilder.toString();

        Map<String, Object> stringObjectMap = jsonParser.parseMap(jsonContent);

        Map<String, Integer> data = new HashMap<>();
        int[] arr = new int[6];
        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {  // 遍历 Map，将数据类型转换为 Integer
            Integer value = Integer.valueOf(entry.getValue().toString());
            if (value >= 0) {
                arr[value] = arr[value] + 1;
            }
            data.put(entry.getKey(), value);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.setData(data);
        String optionJson = optionJsonUtil.getOptionJson(arr);
        resultVo.setOptionJson(optionJson);
        resultVo.setArr(arr);
        return resultVo;
    }

    @ApiOperation("返回结果文件名称，删除模型")
    @DeleteMapping("/delete")
    public String deleteModel(@RequestBody resultFile resultFile) throws IOException {
        if(resultFile!=null){
            String resultFileId = resultFile.getId();
            resultFileservice.removeById(resultFileId);
            String url = resultFile.getUrl();
            cosManager.deleteFile(url);
            return "success";
        }
        return "failed";
    }

}